package org.libermundi.theorcs.chronicles.model.message.impl;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.search.annotations.Indexed;
import org.libermundi.theorcs.chronicles.ChronicleConstants;
import org.libermundi.theorcs.chronicles.model.message.TargetRole;

@Entity(name="InternalEmail")
@Table(name=ChronicleConstants.TBL_INTERNAL_EMAIL)
@Indexed(index="InternalMessage")
public class InternalEmail extends AbstractMessage {
	private static final long serialVersionUID = -2735061412672167618L;

	private InternalEmailTarget _sender;
	private List<InternalEmailTarget> _recipient;
	
	@Transient
	public InternalEmailTarget getSender() {
		if(_sender==null && ! _recipient.isEmpty()){
			for (InternalEmailTarget t : _recipient){
				if(t.getRole()==TargetRole.BY){
					_sender = t;
				}
					break;
			}
		}
		return _sender;
	}

	@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
	@JoinColumn(name="internalEmailId")
	public List<InternalEmailTarget> getRecipient() {
		return _recipient;
	}

	public void setRecipient(List<InternalEmailTarget> recipient) {
		this._recipient = recipient;
	}
}
